package doc2db.interfaces.action;

import doc2db.DbManager.DbSelects;
import doc2db.Keey;
import doc2db.UserProp;
import doc2db.interfaces.mainWindow.MyJEditorPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;

/**
 *
 * @author yura
 */
public class ToTreangleTabAction  extends CreateDialog implements ActionListener{

    public ToTreangleTabAction(JEditorPane __p, JEditorPane t2) {
        super(__p, t2);
    }

    public void actionPerformed(ActionEvent e) {
        if (!Keey.isConnected) {
            return;
        }
        MyJEditorPane mP = (MyJEditorPane) super.p;
        if (mP.isInTable) {
            super.res = mP.tableName;
        }
        if (res.equals("__NULL")) {
            this.show();
        }
        if (res.equals("__NULL")) {
            return;
        }

        String[][] result = null;
        String[] head = null;
        UserProp up = new UserProp();
        String str = "SELECT * FROM \"" + res + "\" ;";
        try {
            DbSelects query = new DbSelects(up.getURL(), up.getUserProp());
            if (query.sendQuery(str)) {
                int rows = query.getRows();
                int cols = query.getColumns();
                result = new String[rows][cols];
                head = new String[cols];
                for (int i = 0; i < head.length; i++) {
                    try {
                        head[i] = "" + query.getMetaData().getColumnName(i + 1);
                    } catch (SQLException exception) {
                    }
                }
                for (int i = 1; i <= rows; i++) {
                    for (int j = 1; j <= cols; j++) {
                        result[i - 1][j - 1] = "" + query.getString(i, j);
                    }
                }
            }
        } catch (Exception ex) {
            if (Keey.debug) 
                System.err.println(ex);
            JOptionPane.showMessageDialog(null, "" + ex, "error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        int ret = JOptionPane.showConfirmDialog(null,"Преобразовать таблицу \"" +
                          res + "\"?", "Подтверждение", JOptionPane.YES_NO_OPTION);
        if(ret == JOptionPane.OK_OPTION){
            String tname = "\"" + res + "_smplmdf\"";
            StringBuffer strB = new StringBuffer("CREATE TABLE " + tname + " " +
                    "(\"name\" varchar, \"value\" varchar, \"type\" varchar);\n");
            try {
                DbSelects query = new DbSelects(up.getURL(), up.getUserProp());
                for (int i = 0; i < result.length; i++) {
                    for (int j = 0; j < result[i].length; j++) {
                        strB.append("INSERT INTO " + tname + " VALUES (")
                            .append("'").append(result[i][0]).append("', ")
                            .append("'").append(result[i][j]).append("', ")
                            .append("'").append(head[j]).append("');\n");
                    }
                }

                if (query.sendQueryUpdate(strB.toString())) {
                    JOptionPane.showMessageDialog(null, "Таблица преобразованна",
                            "message", JOptionPane.WARNING_MESSAGE);
                    String s = "";
                    s += this.e.getText();
                    this.e.setText(s + "\n" + strB.toString());
                    ShowTableAction showTableAction = new ShowTableAction(p, super.e);
                    showTableAction.res = tname.substring(1, tname.length()-1);
                    showTableAction.actionPerformed(null);
                }else{
                    JOptionPane.showMessageDialog(null, "Преобразование не удалось",
                            "error", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception ex) {
                if (Keey.debug) 
                    System.err.println(ex);
                JOptionPane.showMessageDialog(null, "" + ex, "error", JOptionPane.ERROR_MESSAGE);
            }
            res = "__NULL";
        }

    }


}
